import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

class PointerPage extends StatefulWidget {
  const PointerPage({Key? key}) : super(key: key);

  @override
  _PointerPageState createState() => _PointerPageState();
}

class _PointerPageState extends State<PointerPage> {
  double? left = 0;
  double? top = 0;

  double? width = 100;
  double? height = 100;
  double? oneBottom = 0;
  double? twoTop = 0;

  double? w = double.infinity;
  double? h = double.infinity;

  @override
  void initState() {
    super.initState();
    try {
      h = ScreenUtil().screenHeight - kToolbarHeight;
      w = ScreenUtil().screenWidth;
      oneBottom = h;
      if (kDebugMode) {
        print("$h");
        print("$w");
        print("$oneBottom");
      }
    } catch (e) {
      if (kDebugMode) {
        print("异常了：$e");
      }
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('手势'),
      ),
      body: Listener(
        onPointerMove: (event) {
          // print(event.position.dy);
          // kDebugMode.printInfo(info: "$event");
          setState(() {
            oneBottom = event.position.dy;
            twoTop = event.position.dy;
          });
        },
        child: SizedBox(
          // color: Colors.red,
          width: w,
          height: h!,
          child: Stack(children: [
            Positioned(
                bottom: oneBottom,
                left: 0,
                right: 0,
                top: 0,
                child: Container(
                  width: w,
                  height: h,
                  color: Colors.blue,
                )),
            Positioned(
                top: twoTop,
                right: 0,
                bottom: 0,
                left: 0,
                child: Container(
                    color: Colors.green,
                    )),
          ]),
        ),
        // children: getWidget(),
      ),
    );
  }

  List<Widget> getWidget() {
    return <Widget>[
      Positioned.fill(
          child: Container(
        width: 200,
        height: 200,
        color: Colors.blue,
      )),
      // Positioned.fromRect(),
      // Positioned.directional(textDirection: textDirection, child: child),
      // Positioned.fromRelativeRect(rect: rect, child: child),
      Positioned(
        left: left!,
        top: top!,
        child: UnconstrainedBox(
          alignment: Alignment.topLeft,
          child: Listener(
            onPointerMove: (event) {
              setState(() {
                left = event.position.dx - width! / 2;
                top = event.position.dy - (width! / 2 + height!);
                if (kDebugMode) {
                  print("left=$left");
                  print("top=$top");
                }
              });
            },
            onPointerDown: (event) {
              setState(() {
                left = 0;
                top = 0;
                width = 200;
                height = 200;
              });
            },
            onPointerUp: (event) {
              setState(() {
                left = 0;
                top = 0;
                width = 100;
                height = 100;
              });
            },
            child: Container(
              color: Colors.red,
              width: width,
              height: height,
            ),
          ),
        ),
      ),
    ];
  }
}
